<template>
  <div class="login-container">
    <div class="login-header">
      <h2>密钥登录</h2>
      <a href="#" @click.prevent="showAdvancedSettings" class="settings-link">高级设置 ></a>
    </div>
    <form @submit.prevent="handleLogin">
      <div class="form-group">
        <label>SecretID</label>
        <input 
          type="text"
          v-model="formData.secretId"
          placeholder="请输入 API 密钥 SecretID"
          :class="{ 'error': errors.secretId }"
        >
        <div class="error-message" v-if="errors.secretId">{{ errors.secretId }}</div>
      </div>

      <div class="form-group">
        <label>SecretKey</label>
        <input 
          type="password"
          v-model="formData.secretKey"
          placeholder="请输入 API 密钥 SecretKey"
          :class="{ 'error': errors.secretKey }"
        >
        <div class="error-message" v-if="errors.secretKey">{{ errors.secretKey }}</div>
      </div>

      <div class="form-group">
        <label>
          存储桶/收藏路径
          <span class="info-icon" title="非必填，添加访问路径">ⓘ</span>
        </label>
        <input 
          type="text"
          v-model="formData.bucketPath"
          placeholder="非必填，添加访问路径"
        >
      </div>

      <div class="form-group">
        <label>备注</label>
        <input 
          type="text"
          v-model="formData.remark"
          placeholder="非必填，添加备注名，用于账号管理"
        >
      </div>

      <div class="checkbox-group">
        <label>
          <input type="checkbox" v-model="formData.rememberLogin">
          <span>记住会话</span>
        </label>
        <a href="#" @click.prevent="showHistory">历史会话</a>
      </div>

      <button type="submit" class="login-btn" :disabled="isLoading">
        {{ isLoading ? '登录中...' : '登录' }}
      </button>

      <div class="alternative-login">
        <button type="button" @click="loginWithTencent">腾讯云账号登录</button>
        <button type="button" @click="loginWithShare">共享链接登录</button>
      </div>

      <div class="footer-links">
        <a href="#" @click.prevent="getSecretKey">获取密钥</a>
        <a href="#" @click.prevent="showAbout">关于</a>
      </div>
    </form>
  </div>
</template>

<script>
export default {
  name: 'LoginForm',
  data() {
    return {
      formData: {
        secretId: '',
        secretKey: '',
        bucketPath: '',
        remark: '',
        rememberLogin: false
      },
      errors: {},
      isLoading: false
    }
  },
  methods: {
    async handleLogin() {
      this.errors = {}
      
      // 验证
      if (!this.formData.secretId) {
        this.errors.secretId = '请输入 SecretID'
      }
      if (!this.formData.secretKey) {
        this.errors.secretKey = '请输入 SecretKey'
      }
      
      if (Object.keys(this.errors).length > 0) {
        return
      }

      this.isLoading = true
      try {
        await window.electronAPI.login(this.formData)
      } catch (error) {
        console.error('登录失败:', error)
      } finally {
        this.isLoading = false
      }
    },
    async showAdvancedSettings() {
      await window.electronAPI.showAdvancedSettings()
    },
    async showHistory() {
      await window.electronAPI.showHistory()
    },
    async loginWithTencent() {
      console.log('使用腾讯云账号登录')
    },
    async loginWithShare() {
      console.log('使用共享链接登录')
    },
    async getSecretKey() {
      await window.electronAPI.getSecretKey()
    },
    async showAbout() {
      await window.electronAPI.showAbout()
    }
  }
}
</script>

<style scoped>
.login-container {
  width: 100%;
  max-width: 400px;
}

.login-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.5rem;
}

h2 {
  font-size: 1.5rem;
  font-weight: 500;
  color: #333;
  margin: 0;
}

.settings-link {
  color: #0052d9;
  text-decoration: none;
  font-size: 0.875rem;
}

.form-group {
  margin-bottom: 1.25rem;
}

.form-group label {
  display: block;
  margin-bottom: 0.5rem;
  color: #333;
  font-size: 0.875rem;
}

.info-icon {
  color: #999;
  margin-left: 0.25rem;
  cursor: help;
}

input[type="text"],
input[type="password"] {
  width: 100%;
  padding: 0.625rem;
  border: 1px solid #ddd;
  border-radius: 3px;
  font-size: 0.875rem;
  transition: border-color 0.3s;
}

input[type="text"]:focus,
input[type="password"]:focus {
  border-color: #0052d9;
  outline: none;
}

input.error {
  border-color: #ff4d4f;
}

.error-message {
  color: #ff4d4f;
  font-size: 0.75rem;
  margin-top: 0.25rem;
}

.checkbox-group {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.25rem;
}

.checkbox-group label {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  cursor: pointer;
  font-size: 0.875rem;
}

.checkbox-group a {
  color: #0052d9;
  text-decoration: none;
  font-size: 0.875rem;
}

.login-btn {
  width: 100%;
  padding: 0.75rem;
  background: #0052d9;
  color: white;
  border: none;
  border-radius: 3px;
  font-size: 0.875rem;
  cursor: pointer;
  margin-bottom: 1rem;
  transition: background 0.3s;
}

.login-btn:hover {
  background: #0047c3;
}

.login-btn:disabled {
  background: #99b8eb;
  cursor: not-allowed;
}

.alternative-login {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem;
  margin-bottom: 1.5rem;
}

.alternative-login button {
  padding: 0.625rem;
  background: white;
  border: 1px solid #ddd;
  border-radius: 3px;
  cursor: pointer;
  font-size: 0.875rem;
  transition: all 0.3s;
}

.alternative-login button:hover {
  border-color: #0052d9;
  color: #0052d9;
}

.footer-links {
  display: flex;
  justify-content: center;
  gap: 2rem;
}

.footer-links a {
  color: #666;
  text-decoration: none;
  font-size: 0.875rem;
  transition: color 0.3s;
}

.footer-links a:hover {
  color: #0052d9;
}

.input-group {
  transition: transform 0.3s ease;
}

.input-group:focus-within {
  transform: scale(1.02);
}

.submit-button {
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.submit-button:before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(
    120deg,
    transparent,
    rgba(255, 255, 255, 0.3),
    transparent
  );
  transition: 0.5s;
}

.submit-button:hover:before {
  left: 100%;
}
</style> 